﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ProductPlanPrint
{
    public partial class SemiPlanPrint : Form
    {
        Dictionary<string, string> pars;
        DBHelper db;
        HttpAgreement http;
        public SemiPlanPrint(Dictionary<string, string> dic)
        {
            InitializeComponent();
            db = new DBHelper();
            pars = dic;
        }

        private void SemiPlanPrint_Load(object sender, EventArgs e)
        {
            // 加载模板
            CommonMethods.loadPrintTemplte(cb_0_Template);
            // 加载打印机
            CommonMethods.loadPrintDevice(cb_0_Print);
        }

        private void btn_print_Click(object sender, EventArgs e)
        {
            var json = JsonConvert.DeserializeObject<JObject>(pars["pars"]);

            var plCode = json["plCode"].ToString();

            var dt = GetDt(plCode);

            var printDt = new DataTable();

            printDt.Columns.Add("客户");
            printDt.Columns.Add("订单号码");
            printDt.Columns.Add("颜色");
            printDt.Columns.Add("压纹");
            printDt.Columns.Add("厚度/克重");

            printDt.Columns.Add("宽幅");
            printDt.Columns.Add("米长");
            printDt.Columns.Add("支数");
            printDt.Columns.Add("比重");
            printDt.Columns.Add("上限-下限");

            printDt.Columns.Add("总重");
            printDt.Columns.Add("软硬度");
            printDt.Columns.Add("面底");
            printDt.Columns.Add("手数");
            printDt.Columns.Add("中高");

            printDt.Columns.Add("下单日期");

            printDt.Columns.Add("件数");

            printDt.Columns.Add("色号");

            printDt.Columns.Add("克重");


            printDt.Columns.Add("厚度");

            printDt.Columns.Add("存货名称");

            printDt.Columns.Add("备注");

            foreach (DataRow row in dt.Rows)
            {
                var printRow = printDt.NewRow();

                printRow["客户"] = row["cu_customerName"].ToString();
                printRow["订单号码"] = row["plCode"].ToString();
                printRow["颜色"] = row["cu_color"].ToString();
                printRow["压纹"] = row["cu_embossing"].ToString();
                printRow["厚度/克重"] = row["cu_gramThick"].ToString();

                printRow["宽幅"] = row["cu_widthRange"].ToString();
                printRow["米长"] = row["cu_rollLength"].ToString();
                printRow["支数"] = row["cu_branchNum"].ToString();
                printRow["比重"] = row["cu_proportion"].ToString();
                printRow["上限-下限"] = row["cu_upDownLimit"];

                printRow["总重"] = row["cu_totalWeight"].ToString();
                printRow["软硬度"] = row["cu_hardnessDegree"].ToString();
                printRow["面底"] = row["cu_faceBottom"].ToString();
                printRow["手数"] = row["cu_lotSize"].ToString();
                printRow["中高"] = row["cu_MiddleHigh"].ToString();

                printRow["下单日期"] = row["createtime"].ToString();

                printRow["件数"] = row["cu_numberOfPieces"].ToString();

                printRow["色号"] = row["cu_colorCode"].ToString();

                printRow["克重"] = row["cu_gramWeight"].ToString();

                printRow["厚度"] = row["cu_thickness"].ToString();

                var proCode = row["proCode"].ToString();

                var sql = "select * from fun_Inventory_Info where proCode = '" + proCode + "' ";
                var proDt = db.getData(sql, "");

                printRow["存货名称"] = proDt.Rows[0]["proName"].ToString();

                var mainSql = "select * from [pi_ProductionPlan_info] where plCode = '" + plCode + "'";
                var mainDt = db.getData(mainSql, "");

                printRow["备注"] = mainDt.Rows[0]["memo"].ToString();

                printDt.Rows.Add(printRow);
            }
            var info = "";
            var pResult = CommonMethods.DoPrint_horizontal(printDt, cb_0_Template.Text, cb_0_Print.Text, ref info);
        }

        DataTable GetDt(string plCode)
        {
            var sql = "  select * from pi_ProductionPlan_products where plCode = '" + plCode + "' ";
            return db.getData(sql, "");
        }
    }
}
